<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-8-20
 * Time: 下午7:06
 * To change this template use File | Settings | File Templates.
 */
class user extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();

        if ( !$this->AdminIsLogin() ) {
            $this->load->helper('url');
            redirect('/administrator/admin_login/index');
        }
    }

    public function index()
    {
        $this->load->view('administrator/user');
    }

    public function edit()
    {
        $this->load->helper('form');
        $uid = max($this->uri->segment(4, 0), 1);
        $flag = $this->uri->segment(5, false);
        $this->load->model('user/model_user', 'user');
        $user = $this->user->getUserById($uid);
        //print_r($user);
        $this->load->view('administrator/user_create', array('user' => $user, 'flag'=>$flag));
    }

    public function save()
    {
        $input = $this->input->post();
        //print_r($input);
        $input['password'] && $data['password'] = md5(uniqid('', true));
        $data['user_status'] = $input['user_status'];
        //print_r($data);
        $this->db->where('user_id', $input['user_id']);
        $this->db->update('user_info', $data);
        $this->db->close();
        $this->db = null;

        if($input['password'])
        {
            $this->load->database('bbs');
            $salt = mt_rand('100000', '999999');
            $pwd = md5(md5($input['password']) . $salt);
            $this->db->where('uid', $input['user_id']);
            $this->db->update('ucenter_members', array('password' => $pwd, 'salt' => $salt));
            $this->db->close();
        }

        redirect('/administrator/user/edit/'.$input['user_id'].'/save');
        //echo __METHOD__;
    }

    public function search()
    {
        $limit = 50;
        $currentPage = $this->uri->segment(4, 1);
        $offset = ($currentPage - 1) * $limit;
        $input = $this->input->get();
        //echo '<pre>';print_r($input);
        $where = null;
        //$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
        if($input['reg_start_time'])
        {
            $where['create_time >'] = $input['reg_start_time'];
        }
        if($input['red_end_time'])
        {
            $where['create_time <'] = $input['red_end_time'];
        }
//        if($input['login_start_time'])
//        {
//            $where['lastlogintime >'] = strtotime($input['login_start_time']);
//        }
//        if($input['login_end_time'])
//        {
//            $where['lastlogintime <'] = strtotime($input['login_end_time']);
//        }
        if($input['passport'])
        {
            $where['passport'] = $input['passport'];
        }
        if($input['nickname'])
        {
            $where['nickname'] = $input['nickname'];
        }
        $this->load->model('user/model_user', 'user');
        //$this->user->debug();
        $totalNum = $this->user->getUserCount($where);
        $pageHtml = '';
        $data = array();
        if ($totalNum > $limit) { //页数不足一页
            $this->load->library('pagination');
            $config['base_url'] = site_url('/administrator/user/search/');
            $where && $config['suffix'] = ('?' . http_build_query($input));
            $config['total_rows'] = $totalNum;
            $config['per_page'] = $limit;
            $config['num_links'] = 10;
            $config['uri_segment'] = 4;
            $config['use_page_numbers'] = TRUE;
            $config['anchor_class'] = 'class="number"';
            $this->pagination->initialize($config);
            $pageHtml = $this->pagination->create_links();
        }
        $data = $this->user->getUserList($limit, $offset, 'user_id, passport, nickname, user_status, is_activation, create_time', $where, 'user_id DESC');
        $this->load->view('administrator/user', array('input'=>$input, 'users'=>$data, 'page'=>$pageHtml, 'totalNum'=>$totalNum));
    }

    public function export()
    {
        //$limit = 50;
        //$currentPage = $this->uri->segment(4, 1);
        //$offset = ($currentPage - 1) * $limit;
        $input = $this->input->get();
        //echo '<pre>';print_r($input);
        $where = null;
        //$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
        if ($input['reg_start_time']) {
            $where['create_time >'] = $input['reg_start_time'];
        }
        if ($input['red_end_time']) {
            $where['create_time <'] = $input['red_end_time'];
        }
        //        if($input['login_start_time'])
        //        {
        //            $where['lastlogintime >'] = strtotime($input['login_start_time']);
        //        }
        //        if($input['login_end_time'])
        //        {
        //            $where['lastlogintime <'] = strtotime($input['login_end_time']);
        //        }
        if ($input['passport']) {
            $where['passport'] = $input['passport'];
        }
        if ($input['nickname']) {
            $where['nickname'] = $input['nickname'];
        }
        $this->load->model('user/model_user', 'user');
        $totalNum = $this->user->getUserCount($where);
        $data = array();
        if($totalNum)
        {
            $data = $this->user->getUserList($totalNum, 0, 'user_id, passport, nickname, user_status, is_activation, create_time', $where, 'user_id DESC');
        }
        header('Content-type: application/vnd.ms-excel');
        header("Content-Disposition: attachment; filename=\"export_user.csv\"");
        echo iconv('UTF-8', 'gbk', "用户ID, 通行证, 昵称, 用户状态, 是否激活, 注册日期, -\n");
        foreach ($data as $user) {
            foreach($user as $v)
            {
                echo iconv('UTF-8', 'gbk', "\"{$v}\",");
            }
            echo "\n";
        }
    }

    public function game()
    {
        $limit = 50;
        $currentPage = $this->uri->segment(4, 1);
        $offset = ($currentPage - 1) * $limit;
        $input = $this->input->get();
        //echo '<pre>';print_r($input);
        $where = null;
        //$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
        if($input['reg_start_time'])
        {
            $where['create_time >'] = $input['reg_start_time'];
        }
        if($input['red_end_time'])
        {
            $where['create_time <'] = $input['red_end_time'];
        }
        if($input['passport'])
        {
            $where['passport'] = $input['passport'];
        }
        if($input['nickname'])
        {
            $where['nickname'] = $input['nickname'];
        }
        $this->load->model('model_game', 'game');
        //$this->game->debug();
        $totalNum = $this->game->getApplyCount($where);
        $pageHtml = '';
        $data = array();
        if ($totalNum > $limit) { //页数不足一页
            $this->load->library('pagination');
            $config['base_url'] = site_url('/administrator/user/game/');
            $where && $config['suffix'] = ('?' . http_build_query($input));
            $config['total_rows'] = $totalNum;
            $config['per_page'] = $limit;
            $config['num_links'] = 10;
            $config['uri_segment'] = 4;
            $config['use_page_numbers'] = TRUE;
            $config['anchor_class'] = 'class="number"';
            $this->pagination->initialize($config);
            $pageHtml = $this->pagination->create_links();

        }
        $data = $this->game->getApplyList($limit, $offset, '*', $where, 'create_time ASC');
        $this->load->view('administrator/game', array('input'=>$input, 'users'=>$data, 'page'=>$pageHtml, 'totalNum'=>$totalNum));
    }

    public function game_export()
    {
        //$limit = 50;
        //$currentPage = $this->uri->segment(4, 1);
        //$offset = ($currentPage - 1) * $limit;
        $input = $this->input->get();
        //echo '<pre>';print_r($input);
        $where = null;
        //$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
        if($input['reg_start_time'])
        {
            $where['create_time >'] = $input['reg_start_time'];
        }
        if($input['red_end_time'])
        {
            $where['create_time <'] = $input['red_end_time'];
        }
        if($input['passport'])
        {
            $where['passport'] = $input['passport'];
        }
        if($input['nickname'])
        {
            $where['nickname'] = $input['nickname'];
        }
        $this->load->model('model_game', 'game');
        //$this->game->debug();
        $totalNum = $this->game->getApplyCount($where);

        $data = array();
        if($totalNum)
        {
            $data = $this->game->getApplyList($totalNum, 0, '*', $where, 'create_time ASC');
        }
        header('Content-type: application/vnd.ms-excel');
        header("Content-Disposition: attachment; filename=\"export_game_user.csv\"");
        echo iconv('UTF-8', 'gbk', "序号, 通行证, 昵称, 段位, 棋社, 报名时间, -\n");
        foreach ($data as $user) {
            foreach($user as $v)
            {
                echo iconv('UTF-8', 'gbk', "\"{$v}\",");
            }
            echo "\n";
        }
    }
}
